package ve.lm.hrms_cy.Controller;


import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import ve.lm.hrms_cy.Entity.Response.ResponseEntityCY;
import ve.lm.hrms_cy.Entity.User;
import ve.lm.hrms_cy.Service.UserService;
import ve.lm.hrms_cy.Utils.CaptchaUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/users")
public class UserController {


        @Autowired
        private RedisTemplate<String, Object> redisTemplate;

        @Autowired
        UserService userService;

        @GetMapping("/test01")
        public String JwtValidTest(){

            return "有返回值";

        }
        @PostMapping("/login")
        public ResponseEntityCY<String> userLogin(@RequestParam(value="userNameOrEmail" )String userNameOrEmail, @RequestParam(value="password") String password ,@RequestParam("captchaKey") String captchaKey,
                                                  @RequestParam("captchaValue") String captchaValue){


                String correctCaptcha = (String) redisTemplate.opsForValue().get("captcha:" + captchaKey);

                if (correctCaptcha == null) {
                        return new ResponseEntityCY<>("400", false, null, "验证码已过期，请刷新");
                }

                if (!correctCaptcha.equalsIgnoreCase(captchaValue)) {
                        return new ResponseEntityCY<>("400", false, null, "验证码错误");
                }


                String token = userService.userLogin(userNameOrEmail,password);




                if(token==null)
                    return new ResponseEntityCY<>("404",false,token,"未能匹配到用户！");

                return new ResponseEntityCY<>("200",true,token,"登录成功！");

        }

        @GetMapping("/captcha")
        public void getCaptcha(HttpServletResponse response) throws IOException {
                String captchaText = CaptchaUtil.generateText();
                BufferedImage image = CaptchaUtil.generateImage(captchaText);

                String captchaKey = UUID.randomUUID().toString();

                // 存入Redis，5分钟过期
                redisTemplate.opsForValue().set("captcha:" + captchaKey, captchaText, 5, TimeUnit.MINUTES);

                // 返回验证码Key，放header或响应体
                response.setHeader("Captcha-Key", captchaKey);

                response.setContentType("image/png");
                ImageIO.write(image, "png", response.getOutputStream());
        }


        @GetMapping("/checkToken")
        public ResponseEntityCY<String> checkToken(){

                return new ResponseEntityCY<>("200",true,"你过关！","Token有效");

        }

        @PostMapping("/takePermission")
        public ResponseEntityCY<String> takePermission(@RequestParam("userNameOrEmail") String userNameOrEmail , @RequestParam("password") String password){

                return new ResponseEntityCY<> ("200",true,userService.userLoginForPermission(userNameOrEmail,password),"成功获取权限信息！");

        }


        @PostMapping("/takeId")
        public ResponseEntityCY<Long> takeId(@RequestParam("userNameOrEmail") String userNameOrEmail , @RequestParam("password") String password){

                return new ResponseEntityCY<> ("200",true,userService.userLoginForId(userNameOrEmail,password),"成功获取权限信息！");

        }

        @PostMapping("/register")
        public ResponseEntityCY<Boolean> registerUser(@RequestBody User user){


                return userService.registerUser(user);

        }




}
